package com.ganmiao.web.portal.controller;

import com.ganmiao.server.supplier.entity.Product;
import com.ganmiao.server.supplier.entity.Tender;
import com.ganmiao.server.supplier.service.ProductService;
import com.ganmiao.server.supplier.service.TenderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/product")
public class ProductController {

    @Autowired
    private ProductService productService;

    // Create a tender
    @PostMapping("/create")
    public int productCreate(@RequestBody Product product){
        List<Product> products = productService.getAllProducts();
        // check if id already exists
        for (Product p: products) {
            if (product.getId() == p.getId()) {
                return 0;
            }
        }
        return productService.productCreate(product);
    }

    @GetMapping("/get")
    public Product productGet(@RequestParam Integer id){
        return productService.productGet(id);
    }

    @GetMapping("/getAll")
    public List<Product> getAllProducts() {
        return productService.getAllProducts();
    }

    @PutMapping("/put/{id}")
    public void productUpdate(@PathVariable Integer id, @RequestBody Product product) {
        productService.productUpdate(id, product);
    }

    @DeleteMapping("/delete/{id}")
    public void productDelete(@PathVariable Integer id) {
        productService.productDelete(id);
    }
}
